/**
 * 
 */
package com.android.dwt.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.android.dwt.R;
import com.android.dwt.R.id;
import com.android.dwt.R.layout;
import com.android.dwt.R.string;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author xiang
 *
 */
public class RepeatAct extends Activity implements OnClickListener, OnCheckedChangeListener {
	
	private Calendar calCur;
	private Calendar calUntil;
	private Calendar mCalPre;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	
	private Spinner mSpnRepeat;
	private RadioGroup mRgEnd;
	private RadioButton mRbNever;
	private RadioButton mRbTimes;
	private RadioButton mRbUntil;
	private TextView mTvMsg;
	private Dialog mDlg;
	
	private int mRptType = 0;		// default no repeat
	private int mRptEnd = R.id.rb_never;		// default repeat forever
	private String mRptSummary = "";
	private int mCurCheckedId = R.id.rb_never;
	private int mRptTimes = 5;		// repeat times
	private String mStr = "";
	private Boolean mState = false;	// no state changed
	
	private final int DLG_TIMES = 0;
	private final int DLG_UNTIL = 1;
	private final String TAG = "RepeatAct";
	
	
	
	public static final int RESULT_REPEAT = 4;
	private final int INCREMENT = 5;
//	private final int UNTIL_INCREMENT = 10;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.repeat);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_cmn);
		
		calCur = (Calendar) getIntent().getSerializableExtra("init_date");
		calUntil = (Calendar) calCur.clone();
		mCalPre = (Calendar) calCur.clone();
		
		initViews();
	}
	
	private void initViews() {
		
		((TextView) findViewById(R.id.tv_title)).setText("重复");
		((ImageButton) findViewById(R.id.btn_new)).setVisibility(View.INVISIBLE);
		
		mSpnRepeat = (Spinner) findViewById(R.id.spn_repeat);
		mSpnRepeat.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				
				mRptSummary = "";
				mStr = "";
				mState = false;
				
				switch (position) {
					case 0:			// no repeat
						mRptType = 0;
						mRgEnd.setVisibility(View.GONE);
						mTvMsg.setVisibility(View.GONE);
						
						mRptSummary += getString(R.string.no_repeat);
						break;
						
					case 1:			// day
						mRptType = 1;
						mRgEnd.setVisibility(View.VISIBLE);
						
						mRptSummary += getString(R.string.rpt_day);
						break;
						
					case 2:			// week
						mRptType = 2;
						mRgEnd.setVisibility(View.VISIBLE);
						
						mRptSummary += getString(R.string.rpt_week);
						break;
						
					case 3:			// workday
						mRptType = 3;
						mRgEnd.setVisibility(View.VISIBLE);
						
						mRptSummary += getString(R.string.rpt_workday);
						break;
						
					case 4:			// month
						mRptType = 4;
						mRgEnd.setVisibility(View.VISIBLE);
						
						mRptSummary += getString(R.string.rpt_month);
						break;
						
					case 5:			// year
						mRptType = 5;
						mRgEnd.setVisibility(View.VISIBLE);
						
						mRptSummary += getString(R.string.rpt_year);
						break;
						
					default:
						break;
				}
			}
			
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
		
		mRgEnd = (RadioGroup) findViewById(R.id.rg_repeat);
		mRbNever = (RadioButton) findViewById(R.id.rb_never);
		mRbTimes = (RadioButton) findViewById(R.id.rb_times);
		mRbUntil = (RadioButton) findViewById(R.id.rb_until);
		
		mTvMsg = (TextView) findViewById(R.id.tv_repeat_msg);
		mTvMsg.setOnClickListener(this);
		
		((Button) findViewById(R.id.btn_repeat_ok)).setOnClickListener(this);
		((Button) findViewById(R.id.btn_repeat_cal)).setOnClickListener(this);
		
		ImageButton ibReturn = (ImageButton) findViewById(R.id.btn_return);
		ibReturn.setOnClickListener(this);
		ibReturn.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					
					v.setBackgroundResource(R.drawable.back_pressed);
					
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					
					v.setBackgroundResource(R.drawable.back);
				}

				return false;
			}
			
		});
		
		((RadioGroup) findViewById(R.id.rg_repeat)).setOnCheckedChangeListener(this);
		
	}
	
	/*private String format2(int x) {
		String s = "" + x;
		if (s.length() == 1)
			s = "0" + s;
		
		return s;
	}*/
	
	@Override
	protected Dialog onCreateDialog(int id) {
		
		// TODO Auto-generated method stub
//		return super.onCreateDialog(id);
		
		switch (id) {
		
    		case DLG_TIMES:			// repeat times
    			
    			mDlg = new Dialog(this);
    			mDlg.setTitle(getString(R.string.rpt_times));
    			mDlg.setContentView(R.layout.dialog_view);
    			
    			((Button) mDlg.findViewById(R.id.btn_ok)).setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					// TODO Auto-generated method stub
    					EditText et = (EditText) mDlg.findViewById(R.id.dlg_edit_text);
    					mRptTimes = Integer.parseInt(et.getText().toString());
    					mTvMsg.setText("重复 " + mRptTimes + " 次");
    					
    					mStr = mTvMsg.getText().toString();
    					
    					mDlg.dismiss();
    				}
    			});
    			
    			((Button) mDlg.findViewById(R.id.btn_cal)).setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					// TODO Auto-generated method stub
    					mDlg.dismiss();
    				}
    			});
    			
    			mDlg.show();
    			
    			break;
    			
    		case DLG_UNTIL:
    			
    			OnDateSetListener dateListener = new OnDateSetListener() {
    				@Override
    				public void onDateSet(DatePicker datePicker, int year,
    						int month, int dayOfMonth) {
    					
    					calUntil.clear(Calendar.MILLISECOND);
    					calCur.clear(Calendar.MILLISECOND);
    					
    					calUntil.set(Calendar.YEAR, year);
    					calUntil.set(Calendar.MONTH, month);
    					calUntil.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    					
    					if (calUntil.before(calCur)) {
    						
    						Toast.makeText(RepeatAct.this, getString(R.string.msg_until),
    								Toast.LENGTH_LONG).show();
    						showDialog(DLG_UNTIL);
    						
    						mTvMsg.setText("直到 " + sdf.format(mCalPre.getTime()));
    						
    					} else {
    						
    						mTvMsg.setText("直到 " + sdf.format(calUntil.getTime()));
    						mCalPre.setTime(calUntil.getTime());
    						
    						mStr = mTvMsg.getText().toString();
    					}
    					
    				}
    				
    			};
    			
    			mDlg = new DatePickerDialog(this, dateListener,
    					calCur.get(Calendar.YEAR),
    					calCur.get(Calendar.MONTH),
    					calCur.get(Calendar.DAY_OF_MONTH));
    			
    			mDlg.setOnDismissListener(new OnDismissListener() {
    				
    				@Override
    				public void onDismiss(DialogInterface dialog) {
    					// TODO Auto-generated method stub
    					Log.e(TAG, "cancelled the date!");
    				}
    				
    			});
    			
    			break;
    		
    		default:
    			break;
		}
		
		return mDlg;
	}
	
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		// TODO Auto-generated method stub
//		super.onPrepareDialog(id, dialog);
		Log.i(TAG , "onPrepareDialog is called!");
		
		calUntil.clear(Calendar.MILLISECOND);
		calCur.clear(Calendar.MILLISECOND);
		
		switch (id) {
		
			case DLG_TIMES:
				
				EditText et = (EditText) mDlg.findViewById(R.id.dlg_edit_text);
				et.setText((new Integer(mRptTimes)).toString());
				et.selectAll();
				
				break;
				
			case DLG_UNTIL:
				
				((DatePickerDialog) mDlg).updateDate(mCalPre.get(Calendar.YEAR),
						mCalPre.get(Calendar.MONTH),
						mCalPre.get(Calendar.DAY_OF_MONTH));
				break;
				
			default:
				break;
		}
		
	}
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		
		switch (id) {
		
			case R.id.tv_repeat_msg:
				
				if (mCurCheckedId == R.id.rb_times) {
					
					showDialog(DLG_TIMES);
					
				} else if (mCurCheckedId == R.id.rb_until) {
					
					showDialog(DLG_UNTIL);
				}
				
				break;
				
			case R.id.btn_repeat_ok:
				
				Intent itt = new Intent();
				
				if (mRptType!=0 && !mState) {
					
					mRptSummary += getString(R.string.rpt_never);
					
				} else {
					
					mRptSummary += mStr;
				}
				
				itt.putExtra("repeat_summary", mRptSummary);
				itt.putExtra("repeat_type", mRptType);
				
				switch (mCurCheckedId) {
				
					case R.id.rb_never:
						
						itt.putExtra("repeat_end", R.id.rb_never);
						
						break;
						
					case R.id.rb_times:
						
						itt.putExtra("repeat_end", R.id.rb_times);
						itt.putExtra("repeat_times", mRptTimes);
						
						break;
						
					case R.id.rb_until:
						
						itt.putExtra("repeat_end", R.id.rb_until);
						itt.putExtra("repeat_until", calUntil);
						
						break;
					
					default:
						break;
				}
				
				Log.e(TAG, "repeat_summary: "+mRptSummary);
				
				setResult(RESULT_REPEAT, itt);
				finish();
				
				break;
				
			case R.id.btn_return:
			case R.id.btn_repeat_cal:
				finish();
				break;
				
			case R.id.spn_repeat:
				break;
				
			case R.id.btn_ok:
				break;
				
			case R.id.btn_cal:
				
				break;
				
				
			default:
				break;
		}
	}
	
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		
		mStr = "";
		mState = true;
		
		switch(checkedId) {
		
			case R.id.rb_never:
				
				mCurCheckedId = R.id.rb_never;
				mTvMsg.setVisibility(View.GONE);
				
				mStr += " 一直重复";
				
				break;
				
			case R.id.rb_times:
				
				mCurCheckedId = R.id.rb_times;
				mTvMsg.setVisibility(View.VISIBLE);
				mTvMsg.setText(" 重复 5 次");
				
				mStr += mTvMsg.getText().toString();
				
				break;
				
			case R.id.rb_until:
				
				mCurCheckedId = R.id.rb_until;
				mTvMsg.setVisibility(View.VISIBLE);
				
//				Calendar cld = (Calendar) calCur.clone();
				switch(mRptType) {
				
					case 1:
						calUntil.add(Calendar.DAY_OF_MONTH, INCREMENT);
						break;
						
					case 2:
						calUntil.add(Calendar.WEEK_OF_MONTH, INCREMENT);
						break;
						
					case 3:
						calUntil.add(Calendar.WEEK_OF_MONTH, INCREMENT);
						break;
						
					case 4:
						calUntil.add(Calendar.MONTH, INCREMENT);
						break;
						
					case 5:
						calUntil.add(Calendar.YEAR, INCREMENT);
						break;
						
					default:
						break;
				}
				mCalPre.setTime(calUntil.getTime());
				
				mTvMsg.setText("重复 直到 " + sdf.format(calUntil.getTime()));
				
				mStr += mTvMsg.getText().toString();
				
				break;
				
			default:
				break;
		}
	}
}
